// LongMath.cpp: implementation of the CLongMath class.
//
//////////////////////////////////////////////////////////////////////
#define VC_EXTRALEAN		// Exclude rarely-used stuff from Windows headers

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdtctl.h>		// MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>			// MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

typedef unsigned char uint8;
//nclude "basictool.h"
#include "LongMath.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLongMath::CLongMath(unsigned char * szdata,long max)
{
	m_data = (unsigned char *)malloc(max + 5);
	memcpy(m_data,szdata,max + 1);
}

CLongMath::~CLongMath()
{
	free(m_data);
}
CLongMath CLongMath::operator +(const   CLongMath &obj)
{
	//CLongMath asdf((unsigned char *)"",0);

	int count;
	int data1max = strlen((char *)obj.m_data);
	int data2max = strlen((char *)m_data);
	if(data1max > data2max)
	{
		count = data1max;
	}
	else
	{
		count = data2max;
	}
	CLongMath * ret = new CLongMath((unsigned char *)"",count + 1);
	ret->m_data[count] = 0;
	int temp7 = 0;
	int temp5;
	int temp3;
	int temp4;
	int temp6;
	for(int i = 0; i< count; i++)
	{
		temp5 = count - i -1;
		if(i < data1max)
		{
			 int sadf = data1max - i -1;
			 temp3 = obj.m_data[data1max - i -1] - '0';
		}
		else
		{
			 temp3 = 0;
		}
		if(i < data2max)
		{
			 int sadf = data2max - i -1;
			 temp4 = m_data[data2max - i -1] - '0';
		}
		else
		{
			temp4 = 0;
		}
		temp6 = temp3+ temp4 + temp7;
		temp7 = 0;
		if(temp6 > 9)
		{
			temp6 -= 10;
			temp7 = 1;
		}
		ret->m_data[temp5] = temp6 +  '0';
	}
	return *ret;
}
CLongMath CLongMath::operator =(const   CLongMath   &obj)
{
	//CLongMath sdgdsf(arg.m_data,5);
	realloc(this->m_data,strlen((const char *)obj.m_data));
	memcpy(this->m_data,obj.m_data,strlen((const char *)obj.m_data));
	CLongMath * ret = new CLongMath(this->m_data,strlen((const char *)obj.m_data));
	return  *ret;
}
CLongMath CLongMath::operator -(const   CLongMath &obj)
{
		int count;
	int data1max = strlen((char *)m_data);
	int data2max = strlen((char *)obj.m_data);
	if(data1max > data2max)
	{
		count = data1max;
	}
	else
	{
		count = data2max;
	}
	CLongMath * ret = new CLongMath((unsigned char *)"",count);
	ret->m_data[count] = 0;
	int temp7 = 0;
	int temp5;
	int temp3;
	int temp4;
	int temp6;
	int i;
	for(i = 0; i< count; i++)
	{
		temp5 = count - i -1;
		if(i < data1max)
		{
			 int sadf = data1max - i -1;
			 temp3 = m_data[data1max - i -1] - '0';
		}
		else
		{
			 temp3 = 0;
		}
		if(i < data2max)
		{
			 int sadf = data2max - i -1;
			 temp4 = obj.m_data[data2max - i -1] - '0';
		}
		else
		{
			temp4 = 0;
		}
		temp6 = temp3 - temp4 + temp7;
		temp7 = 0;
		if(temp6 < 0)
		{
			temp6 += 10;
			temp7 = -1;
		}
		ret->m_data[temp5] = temp6 +  '0';
	}
	for(i =0;i< count;i++)
	{
		if(ret->m_data[i] != '0')
		{
			int movcount = i;
			for(;i< count;i++)
			{
				ret->m_data[i - movcount] = ret->m_data[i];
				ret->m_data[i]= 0;
			}
		}
		else
		{
			ret->m_data[i] = 0;
		}
	}
	return *ret;
}
